﻿using System;
using NUnit.Framework;
using System.Xml;
using ValidationFramework.Configuration;
using System.Text.RegularExpressions;

using ValidationFramework.Specifications.Tests.Helpers;
using ValidationFramework.Specifications;

namespace ValidationFramework.Tests.Configuration.ConfigReaders
{
    [TestFixture]
    public class MatchesRegexParserTests
    {
        [Test]
        public void Should_Honor_ValidationExpression_Xml_Attribute()
        {
            // Arrange
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml("<rule typeName='RegexRule' validationExpression='aaaaa'/>");
            var validatorXmlElement = xmlDocument.DocumentElement;

            var configReader = new MatchesRegexParser();

            // Act
			var validator = (MatchesRegexSpec)configReader.GetSpecification(validatorXmlElement, FakeRuleContext.GetContext());

            // Assert
            Assert.AreEqual("aaaaa", validator.ValidationExpression);
        }
        [Test]
        public void Should_Honor_RegexOptions_Xml_Attribute()
        {
            // Arrange
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml("<rule typeName='RegexRule' validationExpression='aaaaa' regexOptions='RightToLeft'/>");
            var validatorXmlElement = xmlDocument.DocumentElement;

            var configReader = new MatchesRegexParser();

            // Act
			var validator = (MatchesRegexSpec)configReader.GetSpecification(validatorXmlElement, FakeRuleContext.GetContext());

            // Assert
            Assert.AreEqual("aaaaa", validator.ValidationExpression);
            Assert.AreEqual(RegexOptions.RightToLeft, validator.RegexOptions);
        }


        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Should_Thow_When_Not_Given_ValidationExpression_Xml_Attribute()
        {
            // Arrange
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml("<rule typeName='RegexRule'/>");
            var validatorXmlElement = xmlDocument.DocumentElement;

            var configReader = new MatchesRegexParser();

            // Act
			var validator = (MatchesRegexSpec)configReader.GetSpecification(validatorXmlElement, FakeRuleContext.GetContext());
        }
    }
}
